package com.turing.post.antimoney.impl;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.antimoney.util.AlarmRule;
import com.turing.post.antimoney.util.AlarmRuleBusiness;
import com.turing.post.antimoney.util.AlarmRuleDao;
import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.organization.util.IssueCompany;
import com.turing.post.organization.util.IssueCompanyDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

@Stateless(mappedName = "antimoney.impl.AlarmRuleBusinessImpl")
@Remote
@Local
public class AlarmRuleBusinessImpl implements AlarmRuleBusiness {
	@EJB(mappedName = "antimoney.impl.AlarmRuleDaoImpl")
	private AlarmRuleDao alarmRuleDaoImpl;
	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao commonDaoImpl;
	@EJB(mappedName = "organization.impl.IssueCompanyDaoImpl")
	private IssueCompanyDao issueCompanyDaoImpl;

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;

	@Override
	public void insert(PCPSWebInput webInput, AlarmRule ar)
			throws PCPSApplicationException {
		if (null == ar) {
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"参数为空");
		}
		if (null == webInput || null == webInput.getOperId()) {
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"操作员参数为空");
		}
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"发卡商参数为空");
		}
		String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
		// 验证发卡商
		IssueCompany issueCompany = issueCompanyDaoImpl
				.getIssueCompanyByIssueCompanyNo(issueCompanyNo);
		if (null == issueCompany || "0".equals(issueCompany.getActivate())) {
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"发卡商无效或未被激活");
		}
		if (PCPSUtil.isEmpty(ar.getJNDIName())) {
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"JNDI不能为空");
		}
		ar.setAlarmRuleId(alarmRuleDaoImpl.getMaxOperId());
		ar.setIssueCompanyNo(issueCompanyNo);
		ar.setLastModifyTime(commonDaoImpl.getDatabaseSysTime());
		ar.setLastOperId(webInput.getOperId());
		ar.setStatus("1");
		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Add, "添加预警规则", "预警规则ID:"
						+ ar.getAlarmRuleId() + "JDNI：" + ar.getJNDIName());
		alarmRuleDaoImpl.insert(ar);
	}

	@Override
	public void update(PCPSWebInput webInput, AlarmRule ar)
			throws PCPSApplicationException {
		if (null == ar || null == ar.getAlarmRuleId()) {
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"参数为空");
		}
		if (null == webInput || null == webInput.getOperId()) {
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"操作员参数为空");
		}
		AlarmRule alarmRule = alarmRuleDaoImpl.getAlarmRuleById(ar
				.getAlarmRuleId());
		if (null == alarmRule) {
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"预警规则无效");
		}
		if (PCPSUtil.isEmpty(ar.getJNDIName())) {
			throw new PCPSApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"JNDI不能为空");
		}
		alarmRule.setAlarmRuleDesc(ar.getAlarmRuleDesc());
		alarmRule.setAlarmRuleType(ar.getAlarmRuleType());
		alarmRule.setJNDIName(ar.getJNDIName());
		alarmRule.setRuleParameter(ar.getRuleParameter());
		alarmRule.setStatus(ar.getStatus());
		alarmRule.setLastModifyTime(commonDaoImpl.getDatabaseSysTime());
		alarmRule.setLastOperId(webInput.getOperId());
		alarmRule.setUdsiNo(ar.getUdsiNo());
		alarmRule.setRemark(ar.getRemark());
		alarmRuleDaoImpl.update(alarmRule);
		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Edit, "修改预警规则", "预警规则ID:"
						+ ar.getAlarmRuleId() + "JDNI：" + ar.getJNDIName());
	}
}
